```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MySQL主从复制全面解析 | 数据库高可用架构</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
            background-color: #f9fafb;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .highlight-box {
            border-left: 4px solid #7c3aed;
            background-color: rgba(124, 58, 237, 0.05);
        }
        .first-letter {
            float: left;
            font-size: 3.5rem;
            line-height: 1;
            margin-right: 0.5rem;
            margin-top: 0.3rem;
            color: #7c3aed;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col md:flex-row items-center">
                <div class="md:w-1/2 mb-10 md:mb-0">
                    <h1 class="text-4xl md:text-5xl font-bold leading-tight mb-4">MySQL主从复制</h1>
                    <p class="text-xl opacity-90 mb-6">构建高可用数据库架构的核心技术</p>
                    <div class="flex items-center space-x-4">
                        <div class="bg-white bg-opacity-20 px-4 py-2 rounded-full flex items-center">
                            <i class="fas fa-database mr-2"></i>
                            <span>高可用</span>
                        </div>
                        <div class="bg-white bg-opacity-20 px-4 py-2 rounded-full flex items-center">
                            <i class="fas fa-sync-alt mr-2"></i>
                            <span>数据同步</span>
                        </div>
                        <div class="bg-white bg-opacity-20 px-4 py-2 rounded-full flex items-center">
                            <i class="fas fa-sitemap mr-2"></i>
                            <span>负载均衡</span>
                        </div>
                    </div>
                </div>
                <div class="md:w-1/2">
                    <div class="bg-white bg-opacity-10 p-6 rounded-xl backdrop-filter backdrop-blur-sm">
                        <div class="mermaid">
                            graph TD
                            A[主库 Master] -->|binlog| B[从库 Slave 1]
                            A -->|binlog| C[从库 Slave 2]
                            A -->|binlog| D[从库 Slave 3]
                            B -->|读请求| E[应用服务器 1]
                            C -->|读请求| F[应用服务器 2]
                            D -->|读请求| G[应用服务器 3]
                            style A fill:#7c3aed,stroke:#fff,color:#fff
                            style B fill:#4f46e5,stroke:#fff,color:#fff
                            style C fill:#4f46e5,stroke:#fff,color:#fff
                            style D fill:#4f46e5,stroke:#fff,color:#fff
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto max-w-5xl py-12 px-4 md:px-0">
        <!-- Introduction -->
        <div class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-purple-600 mr-3"></div>
                <h2 class="text-2xl font-bold text-gray-800">什么是MySQL主从复制</h2>
            </div>
            <p class="text-gray-700 mb-6">
                <span class="first-letter">M</span>ySQL 的主从复制（Replication）是指将数据从一个主数据库（Master）复制到一个或多个从数据库（Slave）的过程。这种机制广泛用于读写分离、数据备份、灾难恢复、负载均衡等场景。主从复制不仅提升了系统的扩展性，还增强了数据的可用性和安全性。
            </p>
            <div class="grid md:grid-cols-3 gap-6 mt-8">
                <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                    <div class="text-purple-600 mb-3">
                        <i class="fas fa-exchange-alt text-2xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">读写分离</h3>
                    <p class="text-gray-600">读操作分发到从库，写操作在主库执行，减轻主库负载</p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                    <div class="text-purple-600 mb-3">
                        <i class="fas fa-shield-alt text-2xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">数据备份</h3>
                    <p class="text-gray-600">通过从库备份数据，避免影响主库的正常业务</p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                    <div class="text-purple-600 mb-3">
                        <i class="fas fa-globe-americas text-2xl"></i>
                    </div>
                    <h3 class="font-bold text-lg mb-2">异地容灾</h3>
                    <p class="text-gray-600">在不同地域部署从库，实现异地数据容灾备份</p>
                </div>
            </div>
        </div>

        <!-- 基本原理 -->
        <div class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-purple-600 mr-3"></div>
                <h2 class="text-2xl font-bold text-gray-800">主从复制的基本原理</h2>
            </div>
            <p class="text-gray-700 mb-6">MySQL 主从复制基于 <strong class="text-purple-600">二进制日志（Binary Log）</strong>。当主库上的数据发生变化时，这些变化会记录到二进制日志文件中。然后，从库通过复制二进制日志，将主库上的数据变更同步到从库，确保主从数据的一致性。</p>
            
            <div class="bg-white p-6 rounded-xl shadow-sm mb-8">
                <div class="mermaid">
                    sequenceDiagram
                    participant 主库
                    participant 从库IO线程
                    participant 从库SQL线程
                    主库->>从库IO线程: 发送binlog事件
                    从库IO线程->>从库SQL线程: 写入relay log
                    从库SQL线程->>从库: 执行SQL变更
                </div>
            </div>

            <div class="grid md:grid-cols-3 gap-6">
                <div class="highlight-box p-4">
                    <h3 class="font-bold text-lg mb-2 text-purple-600">主库</h3>
                    <p class="text-gray-700">记录数据变更到二进制日志（binlog）</p>
                </div>
                <div class="highlight-box p-4">
                    <h3 class="font-bold text-lg mb-2 text-purple-600">从库I/O线程</h3>
                    <p class="text-gray-700">读取主库的二进制日志，写入中继日志（relay log）</p>
                </div>
                <div class="highlight-box p-4">
                    <h3 class="font-bold text-lg mb-2 text-purple-600">SQL线程</h3>
                    <p class="text-gray-700">从中继日志读取并执行数据变更，应用到从库</p>
                </div>
            </div>
        </div>

        <!-- 复制模式 -->
        <div class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-purple-600 mr-3"></div>
                <h2 class="text-2xl font-bold text-gray-800">主从复制的模式</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                    <div class="flex justify-between items-center mb-4">
                        <h3 class="font-bold text-lg">异步复制</h3>
                        <div class="bg-purple-100 text-purple-600 px-3 py-1 rounded-full text-sm font-medium">默认模式</div>
                    </div>
                    <p class="text-gray-600 mb-4">主库将数据变更记录到二进制日志后，不会等待从库确认数据已同步，而是立即返回确认给客户端。</p>
                    <div class="border-t border-gray-100 pt-4">
                        <div class="flex items-center text-sm text-gray-500 mb-2">
                            <i class="fas fa-bolt mr-2 text-purple-500"></i>
                            <span>高性能</span>
                        </div>
                        <div class="flex items-center text-sm text-gray-500">
                            <i class="fas fa-exclamation-triangle mr-2 text-purple-500"></i>
                            <span>数据一致性较弱</span>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                    <h3 class="font-bold text-lg mb-4">半同步复制</h3>
                    <p class="text-gray-600 mb-4">主库在提交事务后，会等待至少一个从库确认已接收到二进制日志，才会返回成功信息给客户端。</p>
                    <div class="border-t border-gray-100 pt-4">
                        <div class="flex items-center text-sm text-gray-500 mb-2">
                            <i class="fas fa-balance-scale mr-2 text-purple-500"></i>
                            <span>性能与一致性平衡</span>
                        </div>
                        <div class="flex items-center text-sm text-gray-500">
                            <i class="fas fa-check-circle mr-2 text-purple-500"></i>
                            <span>减少数据丢失风险</span>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm card-hover">
                    <h3 class="font-bold text-lg mb-4">全同步复制</h3>
                    <p class="text-gray-600 mb-4">主库在提交事务后，必须等待所有从库确认已接收到并应用了二进制日志，才会返回成功信息给客户端。</p>
                    <div class="border-t border-gray-100 pt-4">
                        <div class="flex items-center text-sm text-gray-500 mb-2">
                            <i class="fas fa-lock mr-2 text-purple-500"></i>
                            <span>强一致性</span>
                        </div>
                        <div class="flex items-center text-sm text-gray-500">
                            <i class="fas fa-tachometer-alt mr-2 text-purple-500"></i>
                            <span>性能较低</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 拓扑结构 -->
        <div class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-purple-600 mr-3"></div>
                <h2 class="text-2xl font-bold text-gray-800">主从复制的常见拓扑结构</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-600 w-8 h-8 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-project-diagram"></i>
                        </div>
                        <h3 class="font-bold text-lg">单主单从</h3>
                    </div>
                    <p class="text-gray-600 mb-4">一个主库与一个从库，结构简单，适用于小规模应用或测试环境。</p>
                    <div class="mermaid">
                        graph LR
                        A[主库] --> B[从库]
                        style A fill:#7c3aed,stroke:#fff,color:#fff
                        style B fill:#4f46e5,stroke:#fff,color:#fff
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-600 w-8 h-8 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-network-wired"></i>
                        </div>
                        <h3 class="font-bold text-lg">单主多从</h3>
                    </div>
                    <p class="text-gray-600 mb-4">一个主库与多个从库，常用于读写分离和负载均衡。</p>
                    <div class="mermaid">
                        graph TD
                        A[主库] --> B[从库1]
                        A --> C[从库2]
                        A --> D[从库3]
                        style A fill:#7c3aed,stroke:#fff,color:#fff
                        style B fill:#4f46e5,stroke:#fff,color:#fff
                        style C fill:#4f46e5,stroke:#fff,color:#fff
                        style D fill:#4f46e5,stroke:#fff,color:#fff
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-600 w-8 h-8 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-sync-alt"></i>
                        </div>
                        <h3 class="font-bold text-lg">主主复制</h3>
                    </div>
                    <p class="text-gray-600 mb-4">两个主库互为对方的从库，允许双向数据写入。</p>
                    <div class="mermaid">
                        graph LR
                        A[主库1] --> B[主库2]
                        B --> A
                        style A fill:#7c3aed,stroke:#fff,color:#fff
                        style B fill:#7c3aed,stroke:#fff,color:#fff
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-sm">
                    <div class="flex items-center mb-4">
                        <div class="bg-purple-100 text-purple-600 w-8 h-8 rounded-full flex items-center justify-center mr-3">
                            <i class="fas fa-link"></i>
                        </div>
                        <h3 class="font-bold text-lg">链式复制</h3>
                    </div>
                    <p class="text-gray-600 mb-4">多个从库按顺序连接形成链式结构，适合需要多层次数据同步的场景。</p>
                    <div class="mermaid">
                        graph LR
                        A[主库] --> B[从库1]
                        B --> C[从库2]
                        C --> D[从库3]
                        style A fill:#7c3aed,stroke:#fff,color:#fff
                        style B fill:#4f46e5,stroke:#fff,color:#fff
                        style C fill:#4f46e5,stroke:#fff,color:#fff
                        style D fill:#4f46e5,stroke:#fff,color:#fff
                    </div>
                </div>
            </div>
        </div>

        <!-- 常见问题 -->
        <div class="mb-16">
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-purple-600 mr-3"></div>
                <h2 class="text-2xl font-bold text-gray-800">主从复制中的常见问题</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                <div class="grid md:grid-cols-3 divide-x divide-gray-100">
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <div class="w-3 h-3 bg-yellow-500 rounded-full mr-2"></div>
                            <h3 class="font-bold">延迟问题</h3>
                        </div>
                        <p class="text-gray-600 mb-4">主库的数据变更无法即时同步到从库，导致主从库的数据在某些时刻不一致。</p>
                        <ul class="text-sm text-gray-500 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>优化主库的 binlog 写入速度</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>使用 SSD 提升 I/O 性能</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>提高从库的处理能力</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <div class="w-3 h-3 bg-red-500 rounded-full mr-2"></div>
                            <h3 class="font-bold">数据一致性问题</h3>
                        </div>
                        <p class="text-gray-600 mb-4">在异步复制模式下，主库提交的事务可能在从库还未同步完成时出现宕机，导致数据不一致。</p>
                        <ul class="text-sm text-gray-500 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>使用半同步或全同步复制模式</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>在应用层设计冗余机制</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>定期比对主从数据</span>
                            </li>
                        </ul>
                    </div>
                    
                    <div class="p-6">
                        <div class="flex items-center mb-3">
                            <div class="w-3 h-3 bg-orange-500 rounded-full mr-2"></div>
                            <h3 class="font-bold">数据冲突问题</h3>
                        </div>
                        <p class="text-gray-600 mb-4">在主主复制模式下，如果两个主库同时对同一行数据进行不同修改，可能导致数据冲突。</p>
                        <ul class="text-sm text-gray-500 space-y-2">
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>严格控制数据写入规则</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>在应用层通过业务逻辑避免冲突</span>
                            </li>
                            <li class="flex items-start">
                                <i class="fas fa-check-circle text-purple-500 mt-1 mr-2"></i>
                                <span>使用全局ID生成器</span>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <!-- 配置指南 -->
        <div>
            <div class="flex items-center mb-6">
                <div class="w-2 h-8 bg-purple-600 mr-3"></div>
                <h2 class="text-2xl font-bold text-gray-800">实现与配置指南</h2>
            </div>
            
            <div class="bg-white rounded-xl shadow-sm overflow-hidden mb-8">
                <div class="border-b border-gray-100">
                    <div class="flex items-center px-6 py-4 bg-gray-50">
                        <i class="fas fa-server text-purple-500 mr-3"></i>
                        <h3 class="font-bold">主库配置 (my.cnf)</h3>
                    </div>
                    <div class="p-6">
                        <pre class="bg-gray-800 text-gray-100 p-4 rounded-md overflow-x-auto text-sm">
[mysqld]
server-id = 1
log_bin = mysql-bin
binlog_format = ROW
binlog_row_image = FULL
expire_logs_days = 7
sync_binlog = 1</pre>
                    </div>
                </div>
                
                <div class="border-b border-gray-100">
                    <div class="flex items-center px-6 py-4 bg-gray-50">
                        <i class="fas fa-database text-purple-500 mr-3"></i>
                        <h3 class="font-bold">从库配置 (my.cnf)</h3>
                    </div>
                    <div class="p-6">
                        <pre class="bg-gray-800 text-gray-100 p-4 rounded-md overflow-x-auto text-sm">
[mysqld]
server-id = 2
relay_log = mysql-relay-bin
log_slave_updates = 1
read_only = 1
skip_slave_start = 1</pre>
                    </div>
                </div>
                
                <div>
                    <div class="flex items-center px-6 py-4 bg-gray-50">
                        <i class="fas fa-terminal text-purple-500 mr-3"></i>
                        <h3 class="font-bold">从库初始化命令</h3>
                    </div>
                    <div class="p-6">
                        <pre class="bg-gray-800 text-gray-100 p-4 rounded-md overflow-x-auto text-sm">
CHANGE MASTER TO
MASTER_HOST='master_host_name',
MASTER_USER='replication_user',
MASTER_PASSWORD='replication_password',
MASTER_LOG_FILE='recorded_log_file_name',
MASTER_LOG_POS=recorded_log_position;

START SLAVE;</pre>
                    </div>
                </div>
            </div>
            
            <div class="bg-purple-50 border border-purple-100 rounded-xl p-6">
                <div class="flex items-start">
                    <div class="bg-purple-100 text-purple-600 p-2 rounded-full mr-4">
                        <i class="fas fa-lightbulb text-lg"></i>
                    </div>
                    <div>
                        <h3 class="font-bold text-lg text-purple-600 mb-2">专业建议</h3>
                        <p class="text-gray-700">在生产环境中，建议使用半同步复制模式以获得性能和数据安全性的平衡。同时，定期监控<code class="bg-purple-100 text-purple-600 px-1 rounded">SHOW SLAVE STATUS</code>的输出，确保复制延迟在可接受范围内。对于关键业务系统，考虑使用GTID(全局事务标识符)来简化故障转移和主从切换操作。</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC, sans-serif'
        });
        
        // 添加微交互效果
        document.querySelectorAll('.card-hover').forEach(card => {
            card.addEventListener('mouseenter', function() {
                this.style.boxShadow = '0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04)';
            });
            card.addEventListener('mouseleave', function() {
                this.style.boxShadow = '0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06)';
            });
        });
    </script>
</body>
</html>
```